package com.weishu.auth.controller;


import cn.hutool.http.HttpUtil;
import com.weishu.auth.form.SysRegion;
import com.weishu.auth.service.ISysRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 地区表 前端控制器
 * </p>
 *
 * @author 史珺玚
 * @since 2021-11-11
 */
@RestController
@RequestMapping("/region")
public class SysRegionController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ISysRegionService iSysRegionService;

    @RequestMapping("provinces")
    public List<SysRegion> getProvinces(){
        List<SysRegion> provincesList = null;
        if (redisTemplate.hasKey("getprovinces")){
            redisTemplate.opsForList().rightPushAll("getprovinces",iSysRegionService.getProvinces("0"));
        }else {
            provincesList = iSysRegionService.getProvinces("0");
            redisTemplate.opsForList().leftPush("getprovinces",iSysRegionService.getProvinces("0"));
        }

        return provincesList;
    }
    @RequestMapping("mun")
    public List<SysRegion> getMunicipalitys(String mid){
        List<SysRegion> provincesList = iSysRegionService.getProvinces(mid);
        return provincesList;
    }
    @RequestMapping("prefectures")
    public List<SysRegion> getPrefectures(String pid){
        List<SysRegion> provincesList = iSysRegionService.getProvinces(pid);
        return provincesList;
    }
    @RequestMapping("phoneyzm")
    public String phoneyzm(String phone){
        String code = "";

        for (int i = 0; i < 6; i++) {
            code += new Random().nextInt(10)+"";
        }
        String appid ="EUCP-EMY-SMS1-1ZO0I";
        String timestamp =  new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String secretKey = "L48DUB90752T3275";
        String str = appid+secretKey+timestamp;
        String sign = null;
        try {
            sign = DigestUtils.md5DigestAsHex(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String mobiles = phone;
        System.err.println(sign);
        String content = "【想和你搭讪】尊敬的用户，您的验证码为"+code;
        String url = "http://www.btom.cn:8080/simpleinter/sendSMS?appId="+appid+"&timestamp="+timestamp+"&sign="+sign+"&mobiles="+mobiles+"&content="+content;
        System.err.println(code);
        String s = HttpUtil.get(url);
        redisTemplate.opsForValue().set("phoneyzm",code,5, TimeUnit.MINUTES);
        return s;
    }

}
